package cn.mihoyo.org.service.impl;

import cn.mihoyo.base.service.impl.BaseServiceImpl;
import cn.mihoyo.org.domain.Department;
import cn.mihoyo.org.mapper.DepartmentMapper;
import cn.mihoyo.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IDepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService  {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> findTree() {
        List<Department> deptAll = departmentMapper.findAll();
        List<Department> deptTree = new ArrayList<>();
        for (Department dept : deptAll) {
            if (dept.getParentId() == null) {
                deptTree.add(dept);
            }else {
                for (Department dept1 : deptAll){
                    if (dept.getParentId() == dept1.getId()){
                        dept1.getChildren().add(dept);
                    }
                }
            }
        }
        return deptTree;
    }
}
